<?php

/**
 * @desc 响应OMS的相关请求
 * @author zhangweifeng@shopex.cn 15-7-7 上午11:02
 */
class apioms_erp_response {

    /**
     * @desc ：返回所有的会员数据
     * @author jeff 2015/07/05 22:54:19
     * @param 参数:类型
     * @return 返回值类型
     */
    function get_members($params, &$service) {
        $filter = array();
        $b2c_members_mdl = app::get('b2c')->model('members');
        if (!empty($params['last_modify'])) {
            $filter['last_modified|bthan'] = $params['last_modify'];
        }
        $where = $b2c_members_mdl->filter($filter);
        $sql = 'SELECT mem.*,pam.login_account AS uname FROM `sdb_b2c_members` AS mem 
        LEFT JOIN `sdb_pam_members` AS pam ON mem.member_id=pam.member_id WHERE ' . $where . ' 
        GROUP BY mem.member_id';
        return $b2c_members_mdl->db->select($sql);
    }

    /**
     * @desc 取得销售区域
     * @return type
     * @author zhangweifeng@shopex.cn 15-7-8 下午8:38
     */
    function get_salesarea($params, &$service) {
        $filter = array();
        if (!empty($params['last_modify'])) {
            $filter['last_modify|bthan'] = $params['last_modify'];
        }
        return app::get('abimanage')->model('sales_area')->getList('*', $filter);
    }

    /**
     * @desc 取得配送日期配置
     * @return type
     * @author zhangweifeng@shopex.cn 15-7-10 下午12:25
     */
    function get_shipdate($params, &$service) {
        $filter = array();
        if (!empty($params['last_modify'])) {
            $filter['last_modify|bthan'] = $params['last_modify'];
        }
        return app::get('abimanage')->model('shipdate')->getList('*', $filter);
    }

    /**
     * @desc 取得配送时间配置
     * @return type
     * @author zhangweifeng@shopex.cn 15-7-10 下午12:25
     */
    function get_shiptime($params, &$service) {
        $filter = array();
        if (!empty($params['last_modify'])) {
            $filter['last_modify|bthan'] = $params['last_modify'];
        }
        return app::get('abimanage')->model('shiptime')->getList('*', $filter);
    }

    /**
     * author: talent4636@gmail.com
     *   date: 2015-7-20 11:01:08
     *   desc:取得商城中的商品基础数据
      =========================ERP中需要的字段：===========================
      名称            字段名/sdf                              说明
      -------------------------------------------------------------------
      货品ID			goods[product][0][product_id]    		新建时木有
      商品类型   		goods[type][type_id]
      品牌			    goods[brand][brand_id]
      商品名称		    goods[name]
      商品编号		    goods[bn]
      预览图片地址	    goods[picurl]
      计量单位		    goods[unit]
      存在唯一码		goods[serial_number]    				true/false
      简介			    goods[brief]
      隐藏			    goods[visibility]       				default=false
      [hidden]上架	    goods[status]           				only-true
      销售价			goods[product][0][price][price][price]
      成本价			goods[product][0][price][cost][price]
      货号			    goods[product][0][bn]
      条形码			goods[product][0][barcode]
      重量			    goods[product][0][weight]
      {规格暂不考虑}
     * ---=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
     * ---=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
     * ---=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
      $goodsData = array(
      'goods_id' => 2,
      'category' => array(
      'cat_id' => 0,
      ),
      'type' => array(
      'type_id' => 1,
      ),
      'brand' => array(
      'brand_id' => '',
      ),
      'name' => 商品名称2,
      'bn' => 222222,
      'picurl' => '',
      'unit' => g,
      'serial_number' => false,
      'brief' => 222222222222,
      'status' => true,
      'product' => array(
      '0' => array(
      'status' => true,
      'product_id' => 2,
      'price' => array(
      'price' => array(
      'price' => 222.000,
      ),
      'cost' => array(
      'price' => 21.000,
      ),
      'mktprice' => array(
      'price' => 0.000,
      ),
      ),
      'bn' => 222222,
      'barcode' => '',
      'weight' => 10.000,
      'default' => 1,
      'unit' => g,
      'visibility' => true,
      ),
      ),
      'description' => '',
      'visibility' => true,
      'keywords' => array(
      '0' => array(
      'keyword' =>array(
      'res_type' => 'goods',
      ),
      ),
      ),
      'spec' => '',
      'adjunct' => '',
      'tag' => array(
      ),
      'barcode' => '',
      );
      =================================END================================
     */
    function get_product_data($params, &$service) {
        $filter = array();
        if (!empty($params['last_modify'])) {
            $filter['oms_modify|bthan'] = $params['last_modify'];
        }
        //添加筛选条件，为处理商品不同步     by shenkai 20160408
        $filter['is_handle'] = '0';
        $modelProducts = app::get('b2c')->model('products');
        $modelGoods = app::get('b2c')->model('goods');
        $modelProducts2Spec=app::get('b2c')->model('goods_spec_index');
        // //获取本次需要同步商品总数
        $count = $modelGoods->count($filter);
        //取前50个
        $goodsData = $modelGoods->getList('*', $filter,0,300,$orderby='oms_modify asc');
        $data = array();
        if (!empty($goodsData)) {
            foreach ($goodsData as $k => $goods) {
                //补全货品的虚拟货品不需要同步到oms中
                $sql='goods_id='.$goods['goods_id'].' and (barcode is not null and barcode<>"")';
                $productData = $modelProducts->getList('*',array('filter_sql'=>$sql));
                $data[$k] = array(
                    'goods_id' => $goods['goods_id'],
                    'category' => array(
                        'cat_id' => $goods['cat_id'],
                    ),
                    'type' => array(
                        'type_id' => $goods['type_id'],
                    ),
                    'brand' => array(
                        'brand_id' => $goods['brand_id'],
                    ),
                    'name' => $goods['name'],
                    'bn' => $goods['bn'],
                    'oms_modify' => $goods['oms_modify'],
                    'unit' => $goods['unit'],
                    'serial_number' => 'false',
                    'spec_desc'=>$goods['spec_desc'],
                    // 'params'=>$goods['params'],
                    'brief' => $goods['brief'],
                    'status' => 'true',
                    // 'description' => '',
                    'visibility' => 'true',
                    'barcode' => $goods['barcode'],
                    //新增商品属性 2015-09-17 13:42 by Mark
                    'goods_attribute'=>$goods['goods_attribute'],
                    'chang'=>$goods['chang'],
                    'kuan'=>$goods['kuan'],
                    'gao'=>$goods['gao'],
                    'spec'=>array($goods['spec_desc']),
                    'params'=>array($goods['params']),
                    // 'params'=>$goods['params'],
                    // 'picurl'=>$goods['image_default_id'],
//                    'keywords' => array(
//                        '0' => array(
//                            'keyword' =>array(
//                                'res_type' => 'goods',
//                            ),
//                        ),
//                    ),
                   // 'spec' => '',
//                    'adjunct' => '',
//                    'tag' => array(
//                    ),
//                    'barcode' => '',
                );
                foreach ($productData as $pkey => $pro) {
                    $data[$k]['product'][$pkey] = array(
                        'status' => 'true',
                        'product_id' => $pro['product_id'],
                        'price' => array(
                            'price' => array(
                                'price' => $pro['price'],
                            ),
                            'cost' => array(
                                'price' => $pro['cost'],
                            ),
                            'mktprice' => array(
                                'price' => $pro['mktprice'] ? $pro['mktprice'] : '0',
                            ),
                        ),
                        'bn' => $pro['bn'],
                        'spec_info' => $pro['spec_info'],
                        'store'=>$pro['store']?$pro['store']:0,
                        'spec_desc'=>$pro['spec_desc'],
                        'barcode' => $pro['barcode'],
                        'weight' => $pro['weight'] ? $pro['weight'] : '0',
                        'default' => $pro['is_default'] == 'true' ? '1' : '0',
                        'unit' => $pro['unit'],
                        'cContent' => $pro['cContent'],
                        'visibility' => 'true',
                        'spec_index'=>$modelProducts2Spec->getList('*',array('product_id'=>$pro['product_id'])),
                    );
                }
            }
          $data['count'] = $count;
        }
        return $data;
    }

    /**
     * @desc 商品类型
     * @param type $params
     * @param type $service
     * @return type
     */
    function get_goods_type($params, &$service) {
        $filter = array();
         if (!empty($params['last_modify'])) {
            $filter['last_modify|bthan'] = $params['last_modify'];
        }
        $mType = app::get('b2c')->model('goods_type');
        $type=$mType->getList('*',$filter);
        foreach ($type as & $v) {
            $v['brand']=$mType->getType2Brand($v['type_id']);
            $v['spec']=$mType->getType2Spec($v['type_id']);
        }
        return $type;
    }
    
    /**
     * @desc 商品品牌
     * @param type $params
     * @param type $service
     * @return type
     */
    function get_brand($params, &$service) {
        $filter = array();
        if (!empty($params['last_modify'])) {
            $filter['last_modify|bthan'] = $params['last_modify'];
        }
        return app::get('b2c')->model('brand')->getList('*', $filter);
    }
     /**
     * ps ：获取商品规格
     * Time :2016/01/25 13:15:21
     * @author Shenkai
     * @param 参数类型
     * @return 返回值类型
    */
    function get_specification($params, &$service){
        $filter = array();
        if (!empty($params['last_modify'])) {
            $filter['last_modify|bthan'] = $params['last_modify'];
        }
        $mSpec = app::get('b2c')->model('specification');
        $spec=$mSpec->getList('*',$filter);
        foreach ($spec as & $v) {
            $v['spec_value']=$mSpec->getSpecValuesById($v['spec_id']);
        }
        return $spec;
    }
    
    
    /**
     * @desc 更新订单结算状态 
     * @author zhangweifeng@shopex.cn 15-7-20 下午2:47
     */
    function update_order_settlement_status($params, &$service) {
        $update_result = kernel::single('b2c_order_status')->update_settlement($params['order_id'], $msg);
        if (!$update_result) {
            return $service->send_user_error('', $msg);
        }
        return $update_result;
    }

    /**
     * @desc ：ERP打印发货单时调用，修改相关订单的发货状态字段,
     * @author jeff 2015/07/22 10:10:37
     * @param 参数类型
     * @return 返回值类型
     * @desc 
     * 'ship_status' => array(
            'type' => array(
                0 => app::get('b2c')->_('未发货'),
                1 => app::get('b2c')->_('已收货'),
                2 => app::get('b2c')->_('部分发货'),
                3 => app::get('b2c')->_('部分退货'),
                4 => app::get('b2c')->_('已退货'),
                5 => app::get('b2c')->_('配送中') // ERP 已导出给第三方物流公司，配送期间
            ),
    */
   function change_ship_status($params, &$service) {
        $arr_order_id = $params['arr_order_id'];
        if (empty($arr_order_id)) {
            return array('success' => 'true', 'msg' => '成功同步 0 条 EC 订单!', 'params' => $params);
        }
        
        $orders_mdl = app::get('b2c')->model('orders');
        $filter['order_id'] = $arr_order_id;
        $filter['ship_status'] = '0';
        $result = $orders_mdl->update(array('ship_status' => '5'), $filter);
        if (!$result) {
            return array('success' => 'false', 'msg' => '修改失败');
        }
        
        //取得成功条数
        $cnt = count($arr_order_id);
        return array('success' => 'true', 'msg' => '成功同步 ' . $cnt . ' 条ec订单!', 'filter' => $filter);
        
    }

    /**
     * @desc ：ERP撤销发货单时调用，重置相关订单的发货状态字段,
     * @author jeff 2015/07/22 10:10:37
     * @param 参数类型
     * @return 返回值类型
    */
    function reset_ship_status($params, &$service) {
        $arr_order_id = $params['arr_order_id'];
        /*
        0 => app::get('b2c')->_('未发货'),
        1 => app::get('b2c')->_('已发货'),
        2 => app::get('b2c')->_('部分发货'),
        3 => app::get('b2c')->_('部分退货'),
        4 => app::get('b2c')->_('已退货'),
        */
        $ship_status = 0;
        $m = app::get('b2c')->model('orders');
        $filter = array('order_id'=>$arr_order_id);
        $r = $m->update(array('ship_status'=>$ship_status),$filter);
        if(!$r) return array('success'=>'false','msg'=>'修改失败');
        return array('success'=>'true','msg'=>'取消发货状态成功!');
    }

    /**
     * @desc ：重置订单状态为正常状态(erp拒绝退款时触发)
     * @author jeff 2015/08/02 11:29:39
     * @param 参数类型
     * @return 返回值类型
    */
    function reset_order_status($params, &$service) {
        $update_result = kernel::single('b2c_order_status')->reset_order($params['order_bn'], $msg);
        if (!$update_result) {
            return $service->send_user_error('', $msg);
        }
        return $update_result;
    }
    
    /**
     * @desc 同步ERP中的相同账户的密码到EC
     * @time 2015-08-18 20:51
     */
    function sysn_password_erp2ec($params,&$service) {
        return $param;
        
        return array("msg"=>"同步ERP密码到EC相同用户成功！");
    }
    
    /**
     * @desc 同步ERP的数据到EC
     * @time 2015-08-24 15:55
     */
    function sync_erp2ec_by_notice($params,&$service){
        $notice_type=$params['notice_type'];
        $haystack=array('BRANCH','BRANCH_PRODUCT','INVOICE_PROFORMA');
        if(in_array($notice_type, $haystack)){
            switch ($notice_type) {
                //仓库同步消息
                case 'BRANCH':
                    $sync_obj=  kernel::single("b2c_tasks_branch_default");
                    $sync_obj->exec();
                    break;
                //仓库库存同步消息
                case 'BRANCH_PRODUCT':
                    $sync_obj=  kernel::single("b2c_tasks_branch_product");
                    $sync_obj->exec();
                    break;
                //形式发票同步消息
                case 'INVOICE_PROFORMA':
                    $sync_obj=  kernel::single("abimanage_tasks_invoice_proforma");
                    $sync_obj->exec();
                    break;
                default:
                    break;
            }
        }
    }
    
    /**
     * @desc 返回所有的渠道数据
     * @time 2015-08-24 17:36
     * @author Mark
     */
    function get_channel($params,&$service) {
        $filter = array();
        if (!empty($params['last_modify'])) {
            $filter['last_modify|bthan'] = $params['last_modify'];
        }
        return app::get('b2c')->model('channel')->getList('*', $filter);
    }
}
